﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    // Layout = null;
    // var url = Context.Request.GetEncodedPathAndQuery();
    // string d = "";
    //var url = Request.RawUrl;
}
@section css {
    <style>
        .less-query .more {
            display: none;
        }
    </style>
}

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 5px;">
                <form class="layui-form layui-form-pane less-query" id="frmSearch" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keyword" autocomplete="off" class="layui-input">
                            </div>
                        </div> 
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search layui-btn-sm"></i> 搜 索</button>
                            @* <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="btnMoreQuery">
                                <i class="layui-icon layui-icon-down"></i>
                            </button> *@
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                @* <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-add-1"></i>添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i> 删除 </button> *@
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>

    </div>
</div>
@section js {


    <script>
        var baseUrl = '@(Url.Content("~/"))';
        layui.use(['form', 'table'], function () {
            var $t = layui.jquery,
                form = layui.form,
                table = layui.table;
            $("#btnMoreQuery").on("click", function () {
                var t = $(this)
                var frmSearch = $("#frmSearch");
                var icon = t.find('i');
                if (icon.hasClass('layui-icon-up')) {
                    icon.removeClass('layui-icon-up').addClass('layui-icon-down');
                    frmSearch.addClass("less-query").removeClass('more-query');
                } else {
                    icon.addClass('layui-icon-up').removeClass('layui-icon-down');
                    frmSearch.addClass("more-query").removeClass('less-query');
                }
                $(".table-search-fieldset").trigger( "resize" );
                //mainTable.resize();
            });
             var mainTable = table.render({
                id:"configMng",
                elem: '#currentTableId',
                url: '@(Url.Content("~/sys/config/page"))',
                method:'post',
                parseData: function (res) {
                    // console.log(res);
                    return {
                        "code": res.resultCode, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.count, //解析数据长度
                        "data": res.data.items //解析数据列表
                    };
                },
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                height: function () {
                    var height = $(".table-search-fieldset").height();
                    // 69 为各个 margin 的和
                    var resultHeight = $(window).height() - height - 35;
                    if (resultHeight < 300) {

                        return 'auto';
                    }
                    return resultHeight;
                }, // 最大高度减去其他容器已占有的高度差
                cols: [[
                    { type: "checkbox", width: 40 },
                    { field: 'id', width: 80, title: 'ID', sort: true },
                    { field: 'title', width: 80, title: '标题' },
                    { field: 'configKey', width: 80, title: '配置关键字', sort: true },
                    { field: 'configValue', width: 180, title: '配置值' },
                    { field: 'configDes', title: '配置描述', width: 150 },
                    { title: '操作', width: 150, toolbar: '#currentTableBar', align: "left" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                autoSort:false, // 服务端排序
                response: {
                    statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
                }
            });


            $(window).on('resize',function(){
                mainTable.resize();
            });
            $(".table-search-fieldset").on('resize',function(){
                console.log('.table-search-fieldset resize:', $(".table-search-fieldset").height())
                mainTable.resize();
            });

            // 触发排序事件
            table.on('sort(currentTableFilter)', function (obj) {

                var where = obj.config.where;
                where.SortField = obj.field

                if (where.SortField) {
                    where.SortType = obj.type
                }
                var option = table.getOptions('configMng');
                //执行搜索重载
                table.reload('configMng', {
                    initSort: obj, // 记录初始排序，如果不设的话，将无法标记表头的排序状态。
                    where: where
                }, 'data');
            });
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //var result = JSON.stringify(data.field);
                // layer.alert(result, {
                //     title: '最终的搜索信息'
                // });
                // 获取配置项
                var $option = table.getOptions('configMng');
                // console.log('获取配置项', $option);
                // 复制搜索条件
                var where = JSON.parse(JSON.stringify(data.field));
                if ($option.where && $option.where.SortField) {
                    where.SortField = $option.where.SortField
                    where.SortType = $option.where.SortType
                }
                //执行搜索重载
                table.reload('configMng', {
                    page: {
                        curr: 1
                    },
                    where: where
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    // var index = layer.open({
                    //     title: '添加用户',
                    //     type: 2,
                    //     shade: 0.2,
                    //     maxmin: true,
                    //     shadeClose: true,
                    //     area: ['100%', '100%'],
                    //     content: '@(Url.Content("~/sys/config/edit/"))',
                    //     end: function(e){
                    //       //console.log('关闭编辑');
                    //       // 紧紧重新加载数据
                    //       table.reloadData('configMng');
                    //     }
                    // });
                    // $(window).on("resize", function () {
                    //     layer.full(index);
                    // });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                // console.log('数据', data, obj);
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑配置',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/config/edit/"))' + data.id,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                          table.reloadData('configMng');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                        layer.close(index);
                        table.reload('configMng');
                    });
                }
            });

        });
    </script>
}